package com.hjm.project.terminal.section.controller;

import com.google.common.collect.Lists;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.framework.web.page.TableDataInfo;
import com.hjm.project.ActActive.ActActive.domain.ActActive;
import com.hjm.project.ActActive.ActActive.service.IActActiveService;
import com.hjm.project.job.task.service.IJobTaskService;
import com.hjm.project.terminal.manufacturer.service.IManufacturerService;
import com.hjm.project.terminal.section.service.ITerminalSectionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author puyiliang
 * @date create in 2020/12/4 15:24
 */
@Controller
@RequestMapping("/terminal/section")
public class TerminalSectionController extends BaseController {

    private String prefix = "terminal/section";

    @Autowired
    private ITerminalSectionService terminalSectionService;

    @Autowired
    private IActActiveService actActiveService;

    @Autowired
    private IJobTaskService jobTaskService;

    @Resource
    private IManufacturerService manufacturerService;

    @RequiresPermissions("terminal:section:view")
    @GetMapping()
    public String section()
    {
        return prefix + "/section";
    }

    /**
     * 二段达标列表
     */
    @RequiresPermissions("terminal:section:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, String> params)
    {
        startPage();
        List<Map<String, String>> list = terminalSectionService.selectTerminalSectionList(params);
        return getDataTable(list);
    }

    @RequiresPermissions("terminal:section:statistics")
    @RequestMapping("/statistics")
    public String statistics(ModelMap map)
    {
        //查询活动列表
        map.put("active", actActiveService.selectActActiveList(new ActActive()));
        return prefix + "/statistics";
    }

    /**
     * 二段达标汇总
     */
    @RequiresPermissions("terminal:section:statisticsList")
    @PostMapping("/statisticsList")
    @ResponseBody
    public TableDataInfo statisticsList(@RequestParam Map<String, String> params)
    {
        startPage();
        List<Map<String, String>> list = null;
        if (StringUtils.isEmpty(params.get("agentNum"))){
             list = Lists.newArrayList();
        }else{
            list = terminalSectionService.selectTerminalStatisticsList(params);
        }
        return getDataTable(list);
    }


    @RequiresPermissions("terminal:section:threeSection")
    @GetMapping("/threeSection")
    public String threeSection()
    {
        return prefix + "/threeSection";
    }

    /**
     * 三段达标列表
     */
    @RequiresPermissions("terminal:section:threeSectionList")
    @PostMapping("/threeSectionList")
    @ResponseBody
    public TableDataInfo threeSectionList(@RequestParam Map<String, String> params)
    {
        startPage();
        List<Map<String, String>> list = terminalSectionService.selectTerminalThreeSectionList(params);
        return getDataTable(list);
    }

    @RequiresPermissions("terminal:section:threeStatistics")
    @RequestMapping("/threeStatistics")
    public String threeStatistics(ModelMap map)
    {
        //查询活动列表
        map.put("active", actActiveService.selectActActiveList(new ActActive()));
        return prefix + "/threeStatistics";
    }

    /**
     * 二段达标汇总
     */
    @RequiresPermissions("terminal:section:threeStatisticsList")
    @PostMapping("/threeStatisticsList")
    @ResponseBody
    public TableDataInfo threeStatisticsList(@RequestParam Map<String, String> params)
    {
        startPage();
        List<Map<String, String>> list = null;
        if (StringUtils.isEmpty(params.get("agentNum"))){
            list = Lists.newArrayList();
        }else{
            list = terminalSectionService.selectTerminalThreeStatisticsList(params);
        }
        return getDataTable(list);
    }

    @RequiresPermissions("terminal:section:sections")
    @GetMapping("/sections")
    public String sections(ModelMap mmap) throws Exception {
        List<Map> manufacturer = manufacturerService.getManufacturer(ShiroUtils.isAgent() ? ShiroUtils.getLoginAgentNum() : null);
        mmap.put("manufacturer",manufacturer);
        return prefix + "/sections";
    }


    @RequiresPermissions("terminal:section:listSections")
    @PostMapping("/listSections")
    @ResponseBody
    public TableDataInfo listSections(@RequestParam Map<String, String> params)
    {
        Map map = terminalSectionService.selectTerminalSectionSum(params);
        startPage();
        List<Map<String, Object>> list = terminalSectionService.selectTerminalSection(params);
        return getDataTable(list,map);
    }

    @RequiresPermissions("terminal:section:allStatistics")
    @RequestMapping("/allStatistics")
    public String allStatistics(ModelMap map)
    {
        //查询活动列表
        map.put("active", actActiveService.selectActActiveList(new ActActive()));
        return prefix + "/allStatistics";
    }


    @RequiresPermissions("terminal:section:allStatisticsList")
    @PostMapping("/allStatisticsList")
    @ResponseBody
    public TableDataInfo allStatisticsList(@RequestParam Map<String, String> params)
    {
        startPage();
        List<Map<String, Object>> list = null;
        if (StringUtils.isEmpty(params.get("agentNum"))){
            list = Lists.newArrayList();
        }else{
            list = terminalSectionService.selectTerminalStatistics(params);
        }
        return getDataTable(list);
    }


    /**
     * 导出终端达标明细
     */
    @Log(title = "终端达标明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(@RequestParam Map params)
    {
        terminalSectionService.export(params,this.getSysUser().getLoginName());
        return AjaxResult.success("操作成功，前往下载队列查看");
    }


}
